package com.fj.netty;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;

/**
 * @author fj
 * @date 2023/12/13 11:28
 */
public class HelloClient {
    public static void main(String[] args) throws Exception{
        new Bootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel channel) throws Exception {
                        System.out.println("初始化");
                        channel.pipeline().addLast(new StringEncoder());
                    }

                    @Override
                    public void channelActive(ChannelHandlerContext ctx) throws Exception {
                        System.out.println("连接服务器成功");
                        super.channelActive(ctx);
                    }
                })
                .connect("127.0.0.1",6789)
                .sync()
                .channel()
                .writeAndFlush("你好");
    }
}
